package com.yourenbang.lingyun.transaction.web.service.impl.manage;

import com.yourenbang.lingyun.constants.enums.PlatNoEnums;
import com.yourenbang.lingyun.constants.enums.WithDrawStatusEnum;
import com.yourenbang.lingyun.constants.enums.fund.AccountType;
import com.yourenbang.lingyun.constants.enums.pay.PayFromEnum;
import com.yourenbang.lingyun.constants.enums.pay.PayTypeEnum;
import com.yourenbang.lingyun.constants.enums.table.FundAccountWithdrawPaymentBillEnum;
import com.yourenbang.lingyun.core.message.Result;
import com.yourenbang.lingyun.core.model.vo.PageVO;
import com.yourenbang.lingyun.core.support.PageFrom;
import com.yourenbang.lingyun.core.util.ExportExcelUtils;
import com.yourenbang.lingyun.core.util.StringUtils;
import com.yourenbang.lingyun.core.util.date.DateFormat;
import com.yourenbang.lingyun.core.util.date.LocalDateUtils;
import com.yourenbang.lingyun.domain.FundAccountWithdrawPaymentBill;
import com.yourenbang.lingyun.domain.FundAccountWithdrawPaymentBillExample;
import com.yourenbang.lingyun.mapper.FundAccountWithdrawPaymentBillMapper;
import com.yourenbang.lingyun.transaction.mapper.TransactionFundAccountMapper;
import com.yourenbang.lingyun.transaction.mapper.TransactionFundAccountWithdrawPaymentBillMapper;
import com.yourenbang.lingyun.transaction.mapper.TransactionFundAccountWithdrawPaymentMapper;
import com.yourenbang.lingyun.transaction.mapper.TransactionMerchantMapper;
import com.yourenbang.lingyun.transaction.util.CommonUtils;
import com.yourenbang.lingyun.transaction.web.service.fund.AccountSystemService;
import com.yourenbang.lingyun.transaction.web.service.fund.FundService;
import com.yourenbang.lingyun.transaction.web.service.manage.ManageService;
import com.yourenbang.transaction.api.model.dto.fund.FundDetailDTO;
import com.yourenbang.transaction.api.model.dto.manage.*;
import com.yourenbang.transaction.api.model.vo.manage.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class ManageServiceImpl implements ManageService {
    @Autowired
    private TransactionFundAccountWithdrawPaymentMapper fundAccountWithdrawPaymentMapper;
    @Autowired
    private TransactionMerchantMapper transactionMerchantMapper;
    @Autowired
    private TransactionFundAccountMapper transactionFundAccountMapper;
    @Autowired
    private AccountSystemService accountSystemService;
    @Autowired
    private TransactionFundAccountWithdrawPaymentBillMapper transactionFundAccountWithdrawPaymentBillMapper;
    @Autowired
    private FundAccountWithdrawPaymentBillMapper fundAccountWithdrawPaymentBillMapper;
    @Autowired
    private FundService fundService;

    @Override
    public void exportDayWithdrawReport(QueryWithdrawTerDTO dto) {
        String title = "押金提现机具导出-" + DateFormat.DATETIME_COMPACT.format(LocalDateTime.now());

        String[] headers = {"订单号", "到账金额", "支付宝账号", "机具号", "商户电话", "商户姓名", "代理编号",
                "代理姓名", "押金政策编号", "提现状态", "发起时间", "失败原因"};
        String[] fieldNames = {"orderSn", "inAmount", "thirdAccount", "terminalSn", "extMerchantPhone", "extMerchantName", "extAgentId",
                "extAgentName", "merchantPledgeCashPolicyId", "withdrawStatus", "createTime", "errors"};
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = servletRequestAttributes.getRequest();

        HttpServletResponse response = servletRequestAttributes.getResponse();

        ExportExcelUtils.exportExcel(title, headers, fieldNames, this.getDayWithdrawReport(dto).getData(), request, response);

    }

    @Override
    public Result<List<QueryWithdrawTerVO>> getDayWithdrawReport(QueryWithdrawTerDTO dto) {
        if(dto.getPlatNo()==null){
            dto.setPlatNo(PlatNoEnums.TYF.getCode());
        }
        List<QueryWithdrawTerVO> withdrawAndTerVOS = fundAccountWithdrawPaymentMapper.queryWithdrawAndTerminal(dto);
        Result<List<QueryWithdrawTerVO>> result = Result.builder().success();
        result.setData(withdrawAndTerVOS);
        return result;
    }

    @Override
    public void exportDayWithdrawReportWs(Integer dayNum) {
        String title = "网商押金提现-" + DateFormat.DATETIME_COMPACT.format(LocalDateTime.now());

        String[] headers = {"订单号", "到账金额", "支付宝账号","发起时间", "机具号", "商户电话", "商户姓名", "代理编号",
                "代理姓名", "押金政策编号", "渠道"};
        String[] fieldNames = {
                "orderSn",
                "inAmount",
                "thirdAccount",
                "createTime",
                "terminalSn",
                "extMerchantPhone",
                "extMerchantName",
                "extAgentId",
                "extAgentName",
                "merchantPledgeCashPolicyId",
                "payGetway"};
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = servletRequestAttributes.getRequest();

        HttpServletResponse response = servletRequestAttributes.getResponse();

        ExportExcelUtils.exportExcel(title, headers, fieldNames, fundAccountWithdrawPaymentMapper.queryWithdrawTerWs(PlatNoEnums.TYF.getCode(), dayNum), request, response);
    }

    @Override
    public void exportDayWithdrawReportXlf(Integer dayNum) {
        String title = "喜乐付押金提现-" + DateFormat.DATETIME_COMPACT.format(LocalDateTime.now());
        String[] headers = {"订单号", "到账金额", "支付宝账号","发起时间", "机具号", "商户电话", "商户姓名", "代理编号",
                "代理姓名", "押金政策编号", "渠道"};
        String[] fieldNames = {
                "orderSn",
                "inAmount",
                "thirdAccount",
                "createTime",
                "terminalSn",
                "extMerchantPhone",
                "extMerchantName",
                "extAgentId",
                "extAgentName",
                "merchantPledgeCashPolicyId",
                "payGetway"};
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = servletRequestAttributes.getRequest();

        HttpServletResponse response = servletRequestAttributes.getResponse();

        ExportExcelUtils.exportExcel(title, headers, fieldNames, fundAccountWithdrawPaymentMapper.queryWithdrawTerXlf(PlatNoEnums.TYF.getCode(), dayNum), request, response);
    }

    @Override
    public Result<PageVO> getFundAccount(QueryFundAccountDTO dto) {
        dto.setAccountStatus(QueryFundAccountDTO.ACCOUNT_STATUS_NORMAL);
        List<QueryFundAccountVO> fundAccounts = transactionFundAccountMapper.queryFundAccount(dto);
        return Result.builder().successPage(fundAccounts);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void exportSystemAccount(QueryFundAccountDTO dto) {
        dto.setPageNum(1);
        dto.setPageSize(100000);
        Result<PageVO> pageVO = getFundAccount(dto);
        List<QueryFundAccountVO> fundAccounts=pageVO.getData().getList();

        String title = "系统账户导出-" + DateFormat.DATETIME_COMPACT.format(LocalDateTime.now());

        String[] headers = {"账户类型", "姓名", "手机号", "推荐码", "总收入金额", "可提金额", "已提金额", "冻结中金额", "不可提金额", "账户状态", "创建时间"};
        String[] fieldNames = {"accountTypeName", "userName", "phone", "referralCode", "accountIncome", "accountAmountWithdrawable",
                "accountAmountWithdrawed", "accountAmountFrozen", "accountAmountUndepositable", "statusName", "createTime"};
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        ExportExcelUtils.exportExcel(title, headers, fieldNames, fundAccounts, request, response);
    }

    @Override
    public Result<QueryFundAccountDetailVO> getFundAccountDetailAndJournal(FundDetailDTO fundDetailDTO) {
        QueryFundAccountDetailVO queryFundAccountDetailVO = transactionFundAccountMapper.selectFundAccountDetail(fundDetailDTO.getFundAccountId());
        PageFrom.startPage(fundDetailDTO.getPageNum(),fundDetailDTO.getPageSize());
        List<TopupRecordVO> topupRecordVOS = transactionFundAccountMapper.selectTopupRecord(fundDetailDTO,queryFundAccountDetailVO.getPlatNo());
        queryFundAccountDetailVO.setTopupRecords( new PageVO(topupRecordVOS));
        return Result.builder().success(queryFundAccountDetailVO);
    }

    @Override
    public Result<QueryFundAccountDetailVO> getFundAccountDetail(FundDetailDTO fundDetailDTO) {
        QueryFundAccountDetailVO queryFundAccountDetailVO = transactionFundAccountMapper.selectFundAccountDetail(fundDetailDTO.getFundAccountId());

        return Result.builder().success(queryFundAccountDetailVO);
    }

    @Override
    public Result<List<QueryAccountTypeVO>> getFundAccountType(Short platNo) {
        List<QueryAccountTypeVO> queryAccountTypeVOS = transactionFundAccountMapper.queryFundAccountType(platNo);
        return Result.builder().success(queryAccountTypeVOS);
    }

    @Override
    public Result<PageVO> listWithdraw(QueryWithdrawListDTO dto) {
        //结束时间兼容当天
        if(StringUtils.isNotEmpty(dto.getEndCreateTime())){
            dto.setEndCreateTime(LocalDateUtils.getStartOfTomorrow(dto.getEndCreateTime()));
        }
        if(StringUtils.isNotEmpty(dto.getEndSuccessTime())){
            dto.setEndSuccessTime(LocalDateUtils.getStartOfTomorrow(dto.getEndSuccessTime()));
        }
        PageFrom.startPage(dto.getPageNum(), dto.getPageSize());
        List<QueryWithdrawListVO> queryWithdrawListVOS = fundAccountWithdrawPaymentMapper.selectWithdrawList(dto);

        if (CollectionUtils.isEmpty(queryWithdrawListVOS)){
            return Result.builder().successPage(queryWithdrawListVOS);
        }

        queryWithdrawListVOS.forEach(queryWithdrawListVO -> {
            String payGetwayName = PayTypeEnum.getNameByCode(queryWithdrawListVO.getPayGetway());
            queryWithdrawListVO.setPayGetwayName(payGetwayName);
            if(Objects.nonNull(queryWithdrawListVO.getFundAccountChangePlanId()) && WithDrawStatusEnum.WITHDRAW_STATUS_FAIL.getCode().equals(queryWithdrawListVO.getWithdrawStatus())){
                //查询是否已经有解冻
               Integer returnRecordCount = fundAccountWithdrawPaymentMapper.getWithdrawReturnRecordCount(dto.getPlatNo(), queryWithdrawListVO.getOrderSn());
               queryWithdrawListVO.setShowReturnBtn(returnRecordCount<=0);
            }
        });

        return Result.builder().successPage(queryWithdrawListVOS);
    }

    @Override
    public Result<PageVO> listT1WithdrawBill(QueryT1WithdrawBillDTO dto) {
        String beginBatchNumber = null;
        String endBatchNumber = null;
        if (StringUtils.isNotBlank(dto.getBeginCreateTime())) {
            beginBatchNumber = dto.getBeginCreateTime().replace("-","");
        }
        if (StringUtils.isNotBlank(dto.getEndCreateTime())) {
            beginBatchNumber = dto.getBeginCreateTime().replace("-","");
        }

//        PageFrom.startPage(dto.getPageNum(),dto.getPageSize());
//        List<QueryT1WithdrawBillVO> queryT1WithdrawBillVOS = transactionFundAccountWithdrawPaymentBillMapper.queryPaymentBill(beginBatchNumber, endBatchNumber);
        FundAccountWithdrawPaymentBillExample fundAccountWithdrawPaymentBillExample = new FundAccountWithdrawPaymentBillExample();
        FundAccountWithdrawPaymentBillExample.Criteria criteria = fundAccountWithdrawPaymentBillExample.createCriteria();
        if (StringUtils.isNotBlank(beginBatchNumber)) {
            criteria.andBatchNumberGreaterThanOrEqualTo(beginBatchNumber);
        }
        if (StringUtils.isNotBlank(endBatchNumber)) {
            criteria.andBatchNumberLessThanOrEqualTo(endBatchNumber);
        }

        fundAccountWithdrawPaymentBillExample.setOrderByClause("batch_number desc");
        List<FundAccountWithdrawPaymentBill> fundAccountWithdrawPaymentBills = fundAccountWithdrawPaymentBillMapper.selectByExample(fundAccountWithdrawPaymentBillExample);

        //将数据按批次号分组
        Map<String, List<FundAccountWithdrawPaymentBill>> collect = fundAccountWithdrawPaymentBills.parallelStream()
                .collect(Collectors.groupingBy(FundAccountWithdrawPaymentBill::getBatchNumber));

        List<QueryT1WithdrawBillVO> queryT1WithdrawBillVOS = new ArrayList<>();
        //一个批次号一条数据, 按不同平台 类型设置对应字段
        for (List<FundAccountWithdrawPaymentBill> value : collect.values()) {
            QueryT1WithdrawBillVO queryT1WithdrawBillVO = new QueryT1WithdrawBillVO();
            for (FundAccountWithdrawPaymentBill fundAccountWithdrawPaymentBill : value) {
                queryT1WithdrawBillVO.setBatchNumber(fundAccountWithdrawPaymentBill.getBatchNumber());
                queryT1WithdrawBillVO.setBillTime(fundAccountWithdrawPaymentBill.getBatchNumber());
                queryT1WithdrawBillVO.setTotalActualAmount(queryT1WithdrawBillVO.getTotalActualAmount().add(fundAccountWithdrawPaymentBill.getActualAmount()));
                queryT1WithdrawBillVO.setTotalActualCount(queryT1WithdrawBillVO.getTotalActualCount()+fundAccountWithdrawPaymentBill.getTotalActualCount());
                queryT1WithdrawBillVO.setTotalCount(queryT1WithdrawBillVO.getTotalCount()+fundAccountWithdrawPaymentBill.getTotalCount());
                queryT1WithdrawBillVO.setTotalAmount(queryT1WithdrawBillVO.getTotalAmount().add(fundAccountWithdrawPaymentBill.getAmount()));
                queryT1WithdrawBillVO.setPayStatus(fundAccountWithdrawPaymentBill.getPayStatus());
                queryT1WithdrawBillVO.setPayTimeBegin(fundAccountWithdrawPaymentBill.getPayTimeBegin());
                queryT1WithdrawBillVO.setPayTimeEnd(fundAccountWithdrawPaymentBill.getPayTimeEnd());
                if (PlatNoEnums.YXY.isEquals(fundAccountWithdrawPaymentBill.getPlatNo())) {
                    if (FundAccountWithdrawPaymentBillEnum.PayChannelTypeEnum.ALIPAY.isEquals(fundAccountWithdrawPaymentBill.getPayChannelType())){
                        queryT1WithdrawBillVO.setYxyAlipayActualAmount(fundAccountWithdrawPaymentBill.getActualAmount());
                        queryT1WithdrawBillVO.setYxyAlipayAmount(fundAccountWithdrawPaymentBill.getAmount());
                    }else if(FundAccountWithdrawPaymentBillEnum.PayChannelTypeEnum.BANK_CARD.isEquals(fundAccountWithdrawPaymentBill.getPayChannelType())){
                        queryT1WithdrawBillVO.setYxyBankActualAmount(fundAccountWithdrawPaymentBill.getActualAmount());
                        queryT1WithdrawBillVO.setYxyBankAmount(fundAccountWithdrawPaymentBill.getAmount());
                    }
                } else if(PlatNoEnums.KLKS.isEquals(fundAccountWithdrawPaymentBill.getPlatNo())) {
                    if (FundAccountWithdrawPaymentBillEnum.PayChannelTypeEnum.ALIPAY.isEquals(fundAccountWithdrawPaymentBill.getPayChannelType())){
                        queryT1WithdrawBillVO.setKlksAlipayActualAmount(fundAccountWithdrawPaymentBill.getActualAmount());
                        queryT1WithdrawBillVO.setKlksAlipayAmount(fundAccountWithdrawPaymentBill.getAmount());
                    }else if(FundAccountWithdrawPaymentBillEnum.PayChannelTypeEnum.BANK_CARD.isEquals(fundAccountWithdrawPaymentBill.getPayChannelType())){
                        queryT1WithdrawBillVO.setKlksBankActualAmount(fundAccountWithdrawPaymentBill.getActualAmount());
                        queryT1WithdrawBillVO.setKlksBankAmount(fundAccountWithdrawPaymentBill.getAmount());
                    }
                }
            }
            queryT1WithdrawBillVOS.add(queryT1WithdrawBillVO);
        }
        int total = queryT1WithdrawBillVOS.size();

        List<QueryT1WithdrawBillVO> collect1 = queryT1WithdrawBillVOS.stream().skip((dto.getPageNum() - 1) * dto.getPageSize()).limit(dto.getPageSize()).
                collect(Collectors.toList());

        log.info("查询日结账单返回:{},入参:{}",collect1.toString(),dto.toString());
        PageVO pageVO = new PageVO(collect1);
        pageVO.setPageNum(dto.getPageNum());

        //当前条数
        pageVO.setPageSize(dto.getPageSize());

        //总条数
        pageVO.setTotal(total);

        //总页数
        pageVO.setPages(total% dto.getPageSize()==0 ?total/ dto.getPageSize() :  total/ dto.getPageSize() +1);
        return Result.builder().success(pageVO);
    }

    @Override
    public void exportListT1WithdrawBill(ExportT1WithdrawBillDTO dto) {
        String beginBatchNumber = null;
        String endBatchNumber = null;
        if (StringUtils.isNotBlank(dto.getBeginCreateTime())) {
            beginBatchNumber = dto.getBeginCreateTime().replace("-","");
        }
        if (StringUtils.isNotBlank(dto.getEndCreateTime())) {
            endBatchNumber = LocalDateUtils.localDateTimeByFormat(
                    LocalDateUtils.parseLocalDateTimeByDefaultFormat(LocalDateUtils.getStartOfTomorrow(dto.getEndCreateTime())),
                    DateFormat.DATE_COMPACT.getPartten());
        }
        FundAccountWithdrawPaymentBillExample fundAccountWithdrawPaymentBillExample = new FundAccountWithdrawPaymentBillExample();
        fundAccountWithdrawPaymentBillExample.createCriteria().andBatchNumberGreaterThanOrEqualTo(beginBatchNumber)
                .andBatchNumberLessThanOrEqualTo(endBatchNumber);
        fundAccountWithdrawPaymentBillExample.setOrderByClause("batch_number desc");
        List<FundAccountWithdrawPaymentBill> fundAccountWithdrawPaymentBills = fundAccountWithdrawPaymentBillMapper.selectByExample(fundAccountWithdrawPaymentBillExample);

        //将数据按批次号分组
        Map<String, List<FundAccountWithdrawPaymentBill>> collect = fundAccountWithdrawPaymentBills.parallelStream()
                .collect(Collectors.groupingBy(FundAccountWithdrawPaymentBill::getBatchNumber));

        List<QueryT1WithdrawBillVO> queryT1WithdrawBillVOS = new ArrayList<>();
        //一个批次号一条数据, 按不同平台 类型设置对应字段
        for (List<FundAccountWithdrawPaymentBill> value : collect.values()) {
            QueryT1WithdrawBillVO queryT1WithdrawBillVO = new QueryT1WithdrawBillVO();
            for (FundAccountWithdrawPaymentBill fundAccountWithdrawPaymentBill : value) {
                queryT1WithdrawBillVO.setBatchNumber(fundAccountWithdrawPaymentBill.getBatchNumber());
                queryT1WithdrawBillVO.setBillTime(fundAccountWithdrawPaymentBill.getBatchNumber());
                queryT1WithdrawBillVO.setTotalActualAmount(queryT1WithdrawBillVO.getTotalActualAmount().add(fundAccountWithdrawPaymentBill.getActualAmount()));
                queryT1WithdrawBillVO.setTotalActualCount(queryT1WithdrawBillVO.getTotalActualCount()+fundAccountWithdrawPaymentBill.getTotalActualCount());
                queryT1WithdrawBillVO.setTotalCount(queryT1WithdrawBillVO.getTotalCount()+fundAccountWithdrawPaymentBill.getTotalCount());
                queryT1WithdrawBillVO.setTotalAmount(queryT1WithdrawBillVO.getTotalAmount().add(fundAccountWithdrawPaymentBill.getAmount()));
                queryT1WithdrawBillVO.setPayStatus(fundAccountWithdrawPaymentBill.getPayStatus());
                queryT1WithdrawBillVO.setPayTimeBegin(fundAccountWithdrawPaymentBill.getPayTimeBegin());
                queryT1WithdrawBillVO.setPayTimeEnd(fundAccountWithdrawPaymentBill.getPayTimeEnd());
                if (PlatNoEnums.YXY.isEquals(fundAccountWithdrawPaymentBill.getPlatNo())) {
                    if (FundAccountWithdrawPaymentBillEnum.PayChannelTypeEnum.ALIPAY.isEquals(fundAccountWithdrawPaymentBill.getPayChannelType())){
                        queryT1WithdrawBillVO.setYxyAlipayActualAmount(fundAccountWithdrawPaymentBill.getActualAmount());
                        queryT1WithdrawBillVO.setYxyAlipayAmount(fundAccountWithdrawPaymentBill.getAmount());
                    }else if(FundAccountWithdrawPaymentBillEnum.PayChannelTypeEnum.BANK_CARD.isEquals(fundAccountWithdrawPaymentBill.getPayChannelType())){
                        queryT1WithdrawBillVO.setYxyBankActualAmount(fundAccountWithdrawPaymentBill.getActualAmount());
                        queryT1WithdrawBillVO.setYxyBankAmount(fundAccountWithdrawPaymentBill.getAmount());
                    }
                } else if(PlatNoEnums.KLKS.isEquals(fundAccountWithdrawPaymentBill.getPlatNo())) {
                    if (FundAccountWithdrawPaymentBillEnum.PayChannelTypeEnum.ALIPAY.isEquals(fundAccountWithdrawPaymentBill.getPayChannelType())){
                        queryT1WithdrawBillVO.setKlksAlipayActualAmount(fundAccountWithdrawPaymentBill.getActualAmount());
                        queryT1WithdrawBillVO.setKlksAlipayAmount(fundAccountWithdrawPaymentBill.getAmount());
                    }else if(FundAccountWithdrawPaymentBillEnum.PayChannelTypeEnum.BANK_CARD.isEquals(fundAccountWithdrawPaymentBill.getPayChannelType())){
                        queryT1WithdrawBillVO.setKlksBankActualAmount(fundAccountWithdrawPaymentBill.getActualAmount());
                        queryT1WithdrawBillVO.setKlksBankAmount(fundAccountWithdrawPaymentBill.getAmount());
                    }
                }
            }
            queryT1WithdrawBillVOS.add(queryT1WithdrawBillVO);
        }
        List<ExportT1WithdrawBillVO> exportT1WithdrawBillVOS = new ArrayList<>();
        for (QueryT1WithdrawBillVO queryT1WithdrawBillVO : queryT1WithdrawBillVOS) {
            ExportT1WithdrawBillVO exportT1WithdrawBillVO = new ExportT1WithdrawBillVO();
            exportT1WithdrawBillVO.setBillTime(queryT1WithdrawBillVO.getBillTime());
            exportT1WithdrawBillVO.setYxyAli(queryT1WithdrawBillVO.getYxyAlipayActualAmount()+"/"+queryT1WithdrawBillVO.getYxyAlipayAmount());
            exportT1WithdrawBillVO.setKlksAli(queryT1WithdrawBillVO.getYxyAlipayActualAmount()+"/"+queryT1WithdrawBillVO.getYxyAlipayAmount());
            exportT1WithdrawBillVO.setTotalAmount(queryT1WithdrawBillVO.getYxyAlipayActualAmount()+"/"+queryT1WithdrawBillVO.getYxyAlipayAmount());
            exportT1WithdrawBillVO.setTotalCount(queryT1WithdrawBillVO.getYxyAlipayActualAmount()+"/"+queryT1WithdrawBillVO.getYxyAlipayAmount());
            exportT1WithdrawBillVO.setYxyBank(queryT1WithdrawBillVO.getYxyAlipayActualAmount()+"/"+queryT1WithdrawBillVO.getYxyAlipayAmount());
            exportT1WithdrawBillVO.setKlksBank(queryT1WithdrawBillVO.getYxyAlipayActualAmount()+"/"+queryT1WithdrawBillVO.getYxyAlipayAmount());
            exportT1WithdrawBillVO.setPayTimeBegin(LocalDateUtils.formatByDefaultFormat(queryT1WithdrawBillVO.getPayTimeBegin()));
            exportT1WithdrawBillVO.setPayTimeEnd(LocalDateUtils.formatByDefaultFormat(queryT1WithdrawBillVO.getPayTimeEnd()));
            if (FundAccountWithdrawPaymentBillEnum.PayStatusEnum.PAY_SUCCESS.isEquals(queryT1WithdrawBillVO.getPayStatus())){
                exportT1WithdrawBillVO.setPayStatus(FundAccountWithdrawPaymentBillEnum.PayStatusEnum.PAY_SUCCESS.getMsg());
            }else if(FundAccountWithdrawPaymentBillEnum.PayStatusEnum.WAIT_PAY.isEquals(queryT1WithdrawBillVO.getPayStatus())){
                exportT1WithdrawBillVO.setPayStatus(FundAccountWithdrawPaymentBillEnum.PayStatusEnum.WAIT_PAY.getMsg());
            }else if(FundAccountWithdrawPaymentBillEnum.PayStatusEnum.PAYING.isEquals(queryT1WithdrawBillVO.getPayStatus())){
                exportT1WithdrawBillVO.setPayStatus(FundAccountWithdrawPaymentBillEnum.PayStatusEnum.PAYING.getMsg());
            }
            exportT1WithdrawBillVOS.add(exportT1WithdrawBillVO);

        }
        log.info("导出日结账单返回:{},入参:{}",exportT1WithdrawBillVOS.toString(),dto.toString());
        String title = "日结提现统计导出-" + DateFormat.DATETIME_COMPACT.format(LocalDateTime.now());

        String[] headers = {"统计时间", "银小云-支付宝（已发/预发）", "银小云-银行卡（已发/预发）", "快收-支付宝（已发/预发）",
                "快收-银行卡（已发/预发）", "金额汇总（已发/预发）", "笔数汇总（已发/预发）",
                "发放状态","发放开始时间","发放结束时间"};
        String[] fieldNames = {"billTime", "yxyAli", "yxyBank", "klksAli", "klksBank", "totalAmount",
                "totalCount","payStatus", "payTimeBegin","payTimeEnd"};
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = servletRequestAttributes.getRequest();

        HttpServletResponse response = servletRequestAttributes.getResponse();
        ExportExcelUtils.exportExcel(title, headers, fieldNames,
                exportT1WithdrawBillVOS, request, response);
    }

    @Override
    public void exportListWithdraw(ExportWithdrawListDTO dto) {
        if(dto.getPlatNo()==null){
            dto.setPlatNo(PlatNoEnums.YXY.getCode());
        }
        if(StringUtils.isEmpty(dto.getBeginCreateTime()) || StringUtils.isEmpty(dto.getEndCreateTime())){
            return;
        }
        String title = "提现列表导出-" + DateFormat.DATETIME_COMPACT.format(LocalDateTime.now());

        String[] headers = {"商户名", "商户电话", "订单号", "第三方交易单号", "收款账号", "提现金额", "到账金额", "提现状态",
                "发起时间","提款渠道","提款账户"};
        String[] fieldNames = {"merchantName", "phone", "orderSn", "thirdOrderId", "thirdAccount", "amount", "inAmount",
                "withdrawStatusName", "createTime","payGetway","accountName"};
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = servletRequestAttributes.getRequest();

        HttpServletResponse response = servletRequestAttributes.getResponse();

        List<QueryWithdrawListVO> queryWithdrawListVOS = fundAccountWithdrawPaymentMapper.selectExportWithdrawList(dto);
        if (CollectionUtils.isNotEmpty(queryWithdrawListVOS)) {
            queryWithdrawListVOS.forEach(item->{
                item.setAccountName(CommonUtils.getAccountNameByAccountType(item.getAccountType()));
            });
        }
        ExportExcelUtils.exportExcel(title, headers, fieldNames, queryWithdrawListVOS, request, response);
    }

    @Override
    public Result<QueryWithdrawDetailVO> detailWithdraw(Integer fundAccountWithdrawPaymentId, Integer platNo) {
        QueryWithdrawDetailVO queryWithdrawDetailVO = fundAccountWithdrawPaymentMapper.
                selectWithdrawDetail(platNo.shortValue(), fundAccountWithdrawPaymentId);
        if (StringUtils.isNotBlank(queryWithdrawDetailVO.getWithdrawType())) {
            queryWithdrawDetailVO.setWithdrawType(PayFromEnum.getPayFrom(queryWithdrawDetailVO.getWithdrawType()).getName());
        }
        if (StringUtils.isNotBlank(queryWithdrawDetailVO.getPayGetway())) {
            queryWithdrawDetailVO.setPayGetway(PayTypeEnum.getPayType(queryWithdrawDetailVO.getPayGetway()).getName());
        }

        return Result.builder().success(queryWithdrawDetailVO);
    }

    @Override
    public Result<PageVO> listPledge(QueryPledgeListDTO dto) {
        List<QueryPledgeListVO> queryPledgeListVOS = transactionMerchantMapper.selectPledgeList(dto);
        Result<PageVO> pageInfoResult = Result.builder().successPage(queryPledgeListVOS);
        return pageInfoResult;
    }

    @Override
    public void exportListPledge(ExportPledgeListDTO dto) {
        List<QueryPledgeListVO> queryPledgeListVOS = transactionMerchantMapper.exportPledgeList(dto);
        String title = "押金列表导出-" + DateFormat.DATETIME_COMPACT.format(LocalDateTime.now());

        String[] headers = {"商户名", "商户电话", "订单号", "缴纳金额", "应退金额", "缴纳时间", "押金状态"};
        String[] fieldNames = {"merchantName", "phone", "orderSn", "payAmount", "refundAmount", "payTime",
                "pledgeCashStatus"};
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = servletRequestAttributes.getRequest();

        HttpServletResponse response = servletRequestAttributes.getResponse();
        ExportExcelUtils.exportExcel(title, headers, fieldNames,
                queryPledgeListVOS, request, response);

    }

    @Override
    public Result<QueryPledgeDetailVO> detailPledge(String merchantId) {
        QueryPledgeDetailVO queryPledgeDetailVO = transactionMerchantMapper.selectPledgeDetail(merchantId);
        return Result.builder().success(queryPledgeDetailVO);
    }

    @Override
    public Result<Void> topupSysAccount(@Validated @RequestBody TopupSysAccountDTO dto) {
        return fundService.add(dto);
    }
}